
<template>
  <div class="paycontent">
    <pay-cost-header title="预交金缴费">
      <i class="iconback"
         slot="back"
         @click="headBackClick"></i>
    </pay-cost-header>
    <h3 class="paytitle">欢迎使用青海省肿瘤医院自主缴费系统</h3>
    <div class="infobox f-clear">
      <div class="moneyset f-left">
        <h4>自定义预存金额</h4>
        <input type="text"
               v-model="amountVal"
               readonly="readonly"
               @focus="focusClick">
        <p>单笔预存金额最低为100元，最高为20000元</p>
        <p class="overtip"
           v-if="tipflag">{{moneytip}}</p>
        <div class="btns">
          <a class="u-button"
             @click="cancleClick">取消</a>
          <a class="u-button sure"
             @click="sureClick">确认</a>
        </div>
      </div>
      <div class="moneylist f-right">
        <!-- 直接选择金额 -->
        <ul class="moneyvalue f-clear"
            v-if="valueshow">
          <li v-for="(item,index) in moneyvalueList"
              :key="index"
              @click="itemClick(item)">{{item}}</li>
        </ul>

        <!-- 键盘输入金额 -->
        <div class="keybordcontent"
             v-if="!valueshow">
          <a class="back"
             @click="backClick">返回</a>
          <div class="valueinput">
            <div class="keyboard">
              <span v-for="(item,index) in numKey"
                    :key="index"
                    @click="numberClick(item)">{{item}}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <p class="wishtip">祝您早日康复</p>
  </div>
</template>

<script>
import Bus from '../../bus'
import PayCostHeader from '../common/PayCostHeader'
export default {
  name: 'PreMoneySet',
  components: {
    PayCostHeader
  },
  data () {
    return {
      valueshow: true,
      tipflag: false,
      amountVal: '',
      moneytip: '',
      moneyvalueList: ['100', '500', '1000', '2000', '3000', '5000', '10000', '20000'],
      numKey: [1, 2, 3, 4, 5, 6, 7, 8, 9, '清空', 0, '删除']
    }
  },
  beforeDestroy () {
    Bus.$emit('money', Number(this.amountVal))
  },
  methods: {
    itemClick (val) {
      this.amountVal = val
    },
    focusClick () {
      this.valueshow = false
      this.amountVal = ''
    },
    backClick () {
      this.valueshow = true
    },
    numberClick (numVal, index) {
      if (typeof numVal === 'number') { // 输入的是否是数字
        this.amountVal += numVal
        if (this.amountVal.length > 5) {
          this.amountVal = this.amountVal.slice(0, 5)
        }
      } else if (numVal === '删除') { // 点击删除
        this.amountVal = this.amountVal.substring(0, this.amountVal.length - 1) // 从字符串末尾删除一个
      } else { // 点击清空
        this.amountVal = ''
      }
    },
    headBackClick () {
      this.$router.go(-1)
    },
    cancleClick () {
      this.$router.go(-1)
    },
    sureClick () { // 点击'确认',判断金额的值是否在范围内
      if (Number(this.amountVal) > 20000) {
        this.tipflag = true
        this.moneytip = '预存金额超过20000'
      } else if (Number(this.amountVal) < 100) {
        this.tipflag = true
        this.moneytip = '预存金额小于100'
      } else {
        this.$router.push('./sweepcodepay')
      }
    }
  },
  watch: {
    amountVal (val) {
      if (val >= 100) {
        this.tipflag = false
      }
    }
  }
}
</script>

<style style="stylus" scoped>
.paycontent {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: url(../../../static/images/pay/pay.png) 100% 100%;
  background-size: 100% 100%;
}
.iconback {
  display: inline-block;
  width: 0.18rem;
  height: 0.3rem;
  background: url(../../../static/images/pay/back.png) 100% 100%;
  background-size: 100% 100%;
  vertical-align: -0.04rem;
  margin-right: 0.08rem;
  cursor: pointer;
}
.paytitle {
  text-align: center;
  font-size: 0.46rem;
  color: #666;
  font-weight: bold;
  margin-top: 1rem;
  margin-bottom: 0.8rem;
}
.infobox {
  width: 12rem;
  height: 6rem;
  border-radius: 0.2rem;
  background: #f7f8fa;
  box-shadow: 0px 11px 24px 0px rgba(127, 135, 143, 0.1);
  margin: 0 auto;
  overflow: hidden;
}
.infobox .moneyset {
  padding: 0.8rem 0.26rem 0 0.4rem;
  width: 5.9rem;
  position: relative;
}
.infobox .moneyset h4 {
  font-size: 0.3rem;
  color: #49cfbf;
  text-align: center;
  margin-bottom: 0.68rem;
}
.infobox .moneyset input {
  width: 5.26rem;
  height: 0.88rem;
  border: 0.02rem solid #e6e6e6;
  background: #f7f8fa;
  padding: 0 0.3rem;
}
.infobox .moneyset p {
  font-size: 0.24rem;
  color: #999;
  margin: 0.28rem 0 0.9rem 0;
}
.infobox .moneyset .overtip {
  color: #f00;
  font-size: 0.2rem;
  line-height: 1;
  margin: 0;
  position: absolute;
  bottom: 1.4rem;
}
.infobox .moneyset .btns .u-button {
  display: inline-block;
  width: 2rem;
  height: 0.88rem;
  line-height: 0.86rem;
  text-align: center;
  border-radius: 0.12rem;
  border: 0.01rem solid #49cfbf;
  font-size: 0.3rem;
  color: #49cfbf;
}
.infobox .moneyset .btns .u-button:nth-child(1) {
  margin-right: 0.8rem;
  margin-left: 0.22rem;
}
.infobox .moneyset .btns .u-button.sure {
  background: #49cfbf;
  color: #fff;
}
.moneylist {
  width: 6rem;
}
.moneyvalue {
  padding: 0.82rem 0.2rem 0 0.2rem;
}
.moneyvalue li {
  float: left;
  width: 2.4rem;
  height: 0.88rem;
  line-height: 0.88rem;
  text-align: center;
  color: #fff;
  background: #49cfbf;
  margin: 0 0.2rem 0.3rem 0.2rem;
}
.keybordcontent {
  text-align: right;
}
.keybordcontent .back {
  height: 0.9rem;
  line-height: 0.9rem;
  display: inline-block;
  padding: 0 0.45rem;
  font-size: 0.24rem;
  color: #999;
}
.keyboard {
  clear: both;
  overflow: hidden;
  text-align: center;
  background: #fff;
}
.keyboard span {
  height: 1.27rem;
  width: 2rem;
  line-height: 1.27rem;
  font-size: 0.48rem;
  color: #333;
  float: left;
  border-right: 0.02rem solid #e6e6e6;
}
.keyboard span:nth-child(3),
.keyboard span:nth-child(6),
.keyboard span:nth-child(9),
.keyboard span:nth-child(12) {
  border: 0;
}
.keyboard span:nth-child(10),
.keyboard span:nth-child(12) {
  background: #f2f3f5;
}
.wishtip {
  text-align: center;
  font-size: 0.22rem;
  color: #aaa;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0.35rem;
}
</style>
